﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DuAnNH.Fd_class;

namespace DuAnNH
{
    public partial class Frm_PhanQuyen : Form
    {
        public Frm_PhanQuyen()
        {
            InitializeComponent();
        }
        Cls_HeThong ht = new Cls_HeThong();
        private void Frm_PhanQuyen_Load(object sender, EventArgs e)
        {
            LV();
            ht.load_lstUser(lstDanhsach);
        }
        public void Load_LV()
        {
            string str = "select * from TBL_Quyen";
            SqlCommand com = new SqlCommand(str, Coso.con);
            com.ExecuteNonQuery();
        }
        public void LV()
        {
            treePhanQuyen.ExpandAll();
            treePhanQuyen.Nodes.Clear();
            ht.load_tree_chucnang(treePhanQuyen);

            for (int i = 0; i < treePhanQuyen.Nodes.Count; i++)
                treePhanQuyen.Nodes[i].Checked = false;
        }

        private void checkAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < treePhanQuyen.Nodes.Count; i++)
            {
                treePhanQuyen.Nodes[i].Checked = true;
            }
        }

        private void unToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < treePhanQuyen.Nodes.Count; i++)
            {
                treePhanQuyen.Nodes[i].Checked = false;
            }
        }

        private void get_data()
        {
            ht.user = textBox1.Text;
            ht.password = textBox2.Text;
            //ht.loai_nhomnguoi = cboNhomnguoi.SelectedValue.ToString();

            string chuoi_chucnang = "";
            for (int i = 0; i < treePhanQuyen.Nodes.Count; i++)
            {
                if (treePhanQuyen.Nodes[i].Checked)
                    chuoi_chucnang = chuoi_chucnang + treePhanQuyen.Nodes[i].Tag.ToString() + ",";
            }
            ht.chuoi_chucnang = chuoi_chucnang;
        }

        private void Btn_Update_Click(object sender, EventArgs e)
        {
            DialogResult kq = MessageBox.Show("Cập nhật thông tin người dùng này ?", "Thông báo", MessageBoxButtons.YesNo);
            if (kq == DialogResult.Yes)
            {
                get_data();
                if (ht.update_user())
                {
                    //prAdd = false;
                    //prUpdate = false;
                    //treePhanQuyen.Enabled = false;
                    //txtPass.ReadOnly = true;
                    //txtPass.Text = "**********";
                    //btnSua.Text = "Sửa";
                    //btnThem.Text = "Thêm";
                    //btnXoa.Text = "Xoá";
                    //btnThem.Enabled = true;
                    //ht.load_lstUser(lstDanhsach);
                }
            }
        }

        private void lstDanhsach_SelectedIndexChanged(object sender, EventArgs e)
        {
            ht.id_user = lstDanhsach.FocusedItem.SubItems[1].Text;
            textBox1.Text = lstDanhsach.FocusedItem.SubItems[0].Text;
        }
    }
}
